/**********************************************************************************
***
***    Screencasting Teaching System C++ Library
***    Copyright (C) 2011  Jiri Novak <jiri.novak@petriny.net>
***                        Wadi Jalil Maluf <wadijm@gmail.com>
***
***    This file is part of the Screencasting Teaching System C++ Library.
***
***    This library is free software; you can redistribute it and/or modify
***    it under the terms of the GNU General Public License as published by
***   the Free Software Foundation; either version 3 of the License, or
***    (at your option) any later version.
***
***    This library is distributed in the hope that it will be useful,
***    but WITHOUT ANY WARRANTY; without even the implied warranty of
***    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
***   GNU General Public License for more details.
***
***    You should have received a copy of the GNU General Public License
***    along with this library; if not, write to the Free Software
***    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
***
*********************************************************************************/

#include <cassert>
#include <gtkmm.h>
#include <log4cxx/logger.h>
#include <log4cxx/xml/domconfigurator.h>

#include "gui/main_window.h"
#include "../common/resources.h"
#include "serverservice.h"
#include "servercallbackhandler.h"
#include "servercallbackhandlerguiimpl.h"

// define static logger variable
log4cxx::LoggerPtr loggerClientStsMain(log4cxx::Logger::getLogger("main"));

int main(int argc, char **argv) {
  // load configuration file for Log4Cxx
  log4cxx::xml::DOMConfigurator::configure(CLIENT_STS_LOG4CXX_FILE);
  LOG4CXX_INFO (loggerClientStsMain, "client starting...");
  
  // initialize GTK framework
  Gtk::Main kit(argc, argv);
  
  // load GUI definition from file
  Glib::RefPtr<Gtk::Builder> refBuilder = Gtk::Builder::create();
  try {
	  refBuilder->add_from_file(CLIENT_STS_UI_FILE);
  } catch(const Glib::FileError& ex) {
    LOG4CXX_FATAL(loggerClientStsMain, "error in file: " << ex.what());
    return 1;
  } catch(const Gtk::BuilderError& ex) {
	LOG4CXX_FATAL(loggerClientStsMain, "error of Gtk::Builder class: " << ex.what());
    return 1;
  }
  
  // create main window
  MainWindow* mainWindow = 0;
  refBuilder->get_widget_derived("main_window", mainWindow);
  LOG4CXX_DEBUG(loggerClientStsMain, "mainWindow created");
  assert(mainWindow != 0 && "mainWindow pointer is null!");

  // get the server service and start it
  ServerService &serverService = ServerService::getInstance();
  serverService.setHandler(&ServerCallbackHandlerGuiImpl::getInstance());

  // run the event loop
  LOG4CXX_TRACE(loggerClientStsMain, "entering the mainWindow event loop");
  kit.run(*mainWindow);

  // perform clean up
  serverService.stop();
  LOG4CXX_DEBUG(loggerClientStsMain, "serverService stopped");
  serverService.join();
  LOG4CXX_DEBUG(loggerClientStsMain, "serverService joined");
  delete mainWindow;
  LOG4CXX_INFO (loggerClientStsMain, "client finished...");

  return 0;
}
